package com.blb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.mapper.StudentMapper;
import com.blb.pojo.Student;
import com.blb.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateRequired(long id, long age){
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateRequiredException(long id, long age){
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
        throw new RuntimeException("Error");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateRequiresNew(long id, long age){
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateRequiresNewException(long id, long age){
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
        throw new RuntimeException("Error");
    }

    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void updateNested(long id, long age) {
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
    }

    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void updateNestedException(long id, long age) {
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
        throw new RuntimeException("Error");
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateSupports(long id, long age) {
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateSupportsException(long id, long age) {
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
        throw new RuntimeException("Error");
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void updateNotSupportsException(long id, long age) {
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
        throw new RuntimeException("Error");
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)

    public void updateNotSupports(long id, long age) {
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
    }

    @Override
    @Transactional(propagation = Propagation.NEVER)
    public void updateNever(long id, long age) {
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
    }

    @Override
    @Transactional(propagation = Propagation.MANDATORY)
    public void updateMandatory(long id, long age) {
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        studentMapper.updateById(student);
    }
}
